<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Config;
use app\common\model\Recommend as RecommendModel;

/**
 * 推荐接口
 */
class Recommend extends Api
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $cache_time = 30;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 产品推荐接口
     * @ApiMethod   (POST)
     * @param string $channel_code  渠道号
     * @param string $type          推荐类型(shan=闪电下款,di=超低门槛,hei=黑户专享,xiao=小额速贷,index=首页推荐,list=列表推荐)
     * @param string $page          页码(默认1)
     * @param string $num           每页数量(默认10)
     * @param string $sign          sign
     */
    public function lists()
    {
        $post = request()->post();
        $page = !empty($post['page']) ? $post['page'] : 1;
        $num  = !empty($post['num']) ? $post['num'] : 10;

        $result = [];
        if (!in_array($post['type'], array_keys(RecommendModel::$types))) {
            $this->error('type参数不正确');
        }

        if ('index' == $post['type']) {
            $ad = Db::name('banner')->field('appid, name, image, url')->order('weigh desc')->cache($this->cache_time)->limit(5)->select();
            foreach ($ad as $k => $v) {
                $ad[$k]['image'] = cdnurl($v['image']);
            }
            $result['notice'] = Db::name('notice')->field('content')->order('weigh desc')->cache($this->cache_time)->select();
            shuffle($result['notice']);

            foreach ($result['notice'] as $k => $v) {
                if ($k >= 50) break;
                $result['notice'][$k]['content'] = preg_replace_callback('|rgb\((\d+)\,\s+(\d+)\,\s+(\d+)\)|', 
                                            function($matchs) {
                                                return '#'.str_pad(base_convert($matchs[1], 10, 16), 2,'0', STR_PAD_LEFT).str_pad(base_convert($matchs[2], 10, 16), 2,'0', STR_PAD_LEFT).str_pad(base_convert($matchs[3], 10, 16), 2,'0', STR_PAD_LEFT);
                                            }, $v['content']);
            }
            
            $result['ad'] = $ad;
        }
        $lists = Db::table('fa_recommend')->alias('c')
                                          ->field('c.appid, icon_image, name, rate, balance, apply_num, desc, url, loan_time')
                                          ->join('fa_applist a', 'c.appid = a.id')
                                          ->where('type', $post['type'])
                                          ->where('a.status', '1')
                                          ->where('c.status', '1')
                                          ->order('weigh desc')
                                          ->limit($num)
                                          ->page($page)
                                          ->cache($this->cache_time)
                                          ->select();

        foreach ($lists as $k => $v) {
            $lists[$k]['icon_image'] = cdnurl($v['icon_image']);
        }
        $result['list'] = $lists;
        $this->success('success', $result);
    }

    //* @param string $type          统计类型(list=列表,banner=幻灯片,detail=详情)
    /**
     * 统计接口
     * @ApiMethod   (POST)
     * @param string $channel_code  渠道号
     * @param string $appid         应用id
     * @param string $user_id       用户id(登录用户传)
     * @param string $deviceid      设备id
     * @param string $extend        扩展参数(传json字符串)
     * @param string $sign          sign
     */
    public function statistics()
    {
        $post = request()->post();
        $insert['channel_code'] = $post['channel_code'] ?? '';
        $insert['type']         = $post['type'] ?? 'detail';
        $insert['appid']        = $post['appid'] ?? 0;
        $insert['user_id']      = $post['user_id'] ?? '';
        $insert['deviceid']     = $post['deviceid'] ?? '';
        $insert['extend']       = $post['extend'] ?? '';
        $insert['createtime']   = time();
        $insert['ip']           = request()->ip();

        if (empty($insert['appid'])) {
            $this->error('appid不能为空');
        }

        Db::name('statistics')->insert($insert);
        Db::name('applist')->where('appid', $insert['appid'])->update(['apply_num' => Db::raw('apply_num+1'), 'true_apply_num' => Db::raw('true_apply_num+1')]);
        $this->success('success', $insert);
    }
}
